package com.bsoft.mvpmy.ui.login;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bsoft.mvpmy.R;
import com.bsoft.mvpmy.base.BaseActivity;
import com.bsoft.mvpmy.model.ResultBean.Login;
import com.bsoft.mvpmy.ui.home.HomeActivity;
import com.bsoft.mvpmy.utils.FastBlur;
import com.bsoft.mvpmy.utils.Rotate3dAnimation;
import com.bsoft.mvpmy.utils.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.bsoft.mvpmy.R.id.btn_loginOrRegister;
import static com.bsoft.mvpmy.R.id.tv_zhuce;

/**
 * User: ZRJ
 * Date: 2016/12/1
 * Email: 471564517@qq.com
 * Description:
 */
public class LoginActivity extends BaseActivity<LoginContract.Presenter> implements LoginContract.View {

    @Bind(R.id.et_phoneNum)
    EditText etPhoneNum;
    @Bind(R.id.et_password)
    EditText etPassword;
    @Bind(R.id.iv_ifShowPassword)
    ImageView ivIfShowPassword;
    @Bind(R.id.tv_forgetPassword)
    TextView tvForgetPassword;
    @Bind(R.id.ll_main_denglu)
    LinearLayout llMainDenglu;

    boolean if_showPwd = true;
    @Bind(R.id.ll_logo)
    LinearLayout llLogo;
    @Bind(R.id.ll_youmeng)
    RelativeLayout llYoumeng;
    @Bind(R.id.ll_phone)
    LinearLayout llPhone;
    @Bind(R.id.ll_key)
    LinearLayout llKey;
    @Bind(R.id.btn_loginOrRegister)
    Button btnLoginOrRegister;
    @Bind(R.id.ll_denglu)
    LinearLayout llDenglu;
    @Bind(R.id.tv_zhuce)
    TextView tvZhuce;
    @Bind(R.id.ll_all)
    RelativeLayout llAll;
    @Bind(R.id.fl_content)
    FrameLayout flContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        initApi();
        this.createPresenter(new LoginPresenter(this));


        int scaleRatio = 10;
        int blurRadius = 5;

        Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.beijing);

        Bitmap scaledBitmap = Bitmap.createScaledBitmap(mBitmap,
                mBitmap.getWidth() / scaleRatio,
                mBitmap.getHeight() / scaleRatio,
                false);
        Bitmap blurBitmap = FastBlur.doBlur(scaledBitmap, blurRadius, true);

        BitmapDrawable bd=new BitmapDrawable(blurBitmap);
        flContent.setBackground(bd);


    }


    @Override
    public void hideLoading() {

    }

    @Override
    public void loginSuccess(Login login) {

        ToastUtils.showToast("登陆成功" + "返回数据：" + login.toString());
        skipAct(HomeActivity.class);
        overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);

    }

    public void showAnimation(View mView) {
        final float centerX = mView.getWidth() / 2.0f;
        final float centerY = mView.getHeight() / 2.0f;
        // 这个是设置需要旋转的角度，我设置的是180度
        Rotate3dAnimation rotateAnimation = new Rotate3dAnimation(0, 360,
                centerX, centerY, 0, false);
        // 这个是设置通话时间的
        rotateAnimation.setDuration(100);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setInterpolator(new AccelerateInterpolator());

        mView.startAnimation(rotateAnimation);
    }


    public boolean islogin() {
        if (TextUtils.isEmpty(etPhoneNum.getText().toString())) {
            ToastUtils.showToast("请输入手机号");
            return false;
        }
        if (TextUtils.isEmpty(etPassword.getText().toString())) {
            ToastUtils.showToast("请输入密码");
            return false;
        }
        return true;
    }

    @OnClick({R.id.iv_ifShowPassword, btn_loginOrRegister, tv_zhuce, R.id.tv_forgetPassword})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_ifShowPassword:
                if_showPwd = !if_showPwd;
                if (if_showPwd) {
                    etPassword
                            .setTransformationMethod(PasswordTransformationMethod
                                    .getInstance());// 设置编辑框为密码模式
                    ivIfShowPassword.setBackgroundResource(R.drawable.eyes_pressed);
                } else {
                    etPassword
                            .setTransformationMethod(HideReturnsTransformationMethod
                                    .getInstance());// 设置编辑框为非密码模式
                    ivIfShowPassword.setBackgroundResource(R.drawable.eyes_normor);
                }
                etPassword
                        .setSelection(etPassword.getText().length());// 控制游标在末尾
                break;
            case btn_loginOrRegister:
                if (btnLoginOrRegister.getText().toString().equals("登录")) {
//                    if (islogin()) {
//                        LoginParams loginParams = new LoginParams(etPhoneNum.getText().toString(), etPassword.getText().toString());
//                        presenter.login(loginParams);
//                    }
                    ToastUtils.showToast("登陆成功");
                    skipAct(HomeActivity.class);
                }
                break;
            case tv_zhuce:
                if (tvZhuce.getText().toString().equals("已注册")) {
                    tvZhuce.setText("注册");
                    btnLoginOrRegister.setText("登录");

                    tvForgetPassword.setVisibility(View.VISIBLE);

                    showAnimation(llDenglu);
                } else {
                    tvZhuce.setText("已注册");
                    btnLoginOrRegister.setText("注册");
                    tvForgetPassword.setVisibility(View.GONE);

                    showAnimation(llDenglu);
                }

                break;
            case R.id.tv_forgetPassword:
                break;
        }
    }
}
